package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.util.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    public void clearImg() {

        Jedis resource = jedisPool.getResource();

        //多的那个要放到前面
        Set<String> sdiff = resource.sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (sdiff != null) {
            for (String fileName : sdiff) {
                QiniuUtils.deleteFileFromQiniu(fileName);
                resource.srem(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
            }
        }


        //清理体检地址的图片信息
        Set<String> diff = resource.sdiff(RedisConstant.IMAGE_URL_ADDRESS_QINIU , RedisConstant.IMAGE_URL_ADDRESS_DB);

        if (diff != null) {

            for (String fileName : diff) {
                QiniuUtils.deleteFileFromQiniu(fileName);
                resource.srem(RedisConstant.IMAGE_URL_ADDRESS_QINIU, fileName);
            }
        }

        resource.close();
    }
}
